package com.zhangyang.esper.view.custom;

import com.alibaba.fastjson.JSONObject;

import com.espertech.esper.client.*;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.client.time.CurrentTimeEvent;
import com.espertech.esper.client.util.DateTime;
import com.espertech.esper.metrics.instrumentation.InstrumentationHelper;
import com.zhangyang.esper.SupportConfigFactory;
import com.zhangyang.esper.support.bean.SupportBean;
import com.zhangyang.esper.support.bean.SupportBeanTimestamp;
import junit.framework.TestCase;

import java.util.Iterator;
import java.util.List;

/**
 * @author Dell
 * @Title:
 * @Description: 请写注释类
 * @date 2022/6/24
 */
public class TestTimeIntervalCustom extends TestCase {

    public EPServiceProvider epService;
    public SupportUpdateListener testListener;

    public void setUp()
    {
        epService = EPServiceProviderManager.getDefaultProvider(SupportConfigFactory.getConfiguration());
        epService.initialize();
        if (InstrumentationHelper.ENABLED) { InstrumentationHelper.startTest(epService, this.getClass(), getName());}
        epService.getEPAdministrator().getConfiguration().addEventType("SupportBean", SupportBean.class);
    }

    protected void tearDown() throws Exception {
        if (InstrumentationHelper.ENABLED) { InstrumentationHelper.endTest();}
        testListener = null;
    }


    public void testTimeExt(){
        testListener = new SupportUpdateListener();

        epService.getEPAdministrator().getConfiguration().addEventType(SupportBeanTimestamp.class);
        EPStatement stmt = epService.getEPAdministrator().createEPL("select  * from SupportBean.ext:time_order(longPrimitive, 1 month).win:ext_timed(longPrimitive, 1 month) order by longPrimitive");
        stmt.addListener(testListener);

        sendExtTimeEvent(DateTime.parseDefaultMSec("2002-02-01T9:00:00.000"), "E1");
        sendExtTimeEvent(DateTime.parseDefaultMSec("2002-03-01T9:00:00.000") - 1, "E2");

        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T8:30:00.000")));
        sendEvent("E4", DateTime.parseDefaultMSec("2002-03-01T8:30:00.000"));
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T8:00:00.000")));
        sendEvent("E2", DateTime.parseDefaultMSec("2002-03-01T8:00:00.000"));

//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-05T10:00:00.000" + 1)));
        sendEvent("E3", DateTime.parseDefaultMSec("2002-03-05T10:00:00.000") + 1);




//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T9:00:00.000") + 1));

        Iterator<EventBean> iterator = stmt.iterator();
        EventBean[] eventBeans = EPAssertionUtil.iteratorToArray(iterator);


        List<EventBean[]> andResetLastNewData = testListener.getNewDataList();
        List<EventBean[]> lastOldData = testListener.getOldDataList();
        for (EventBean[] andResetLastNewDatum : andResetLastNewData) {
            for (EventBean eventBean : andResetLastNewDatum) {
                System.err.println(JSONObject.toJSONString(eventBean));
            }

        }
    }

    protected SupportBeanTimestamp sendEvent(String id, long timestamp)
    {
        SupportBeanTimestamp theEvent = new SupportBeanTimestamp(id, timestamp);
        EPRuntime runtime = epService.getEPRuntime();
        runtime.sendEvent(theEvent);
        return theEvent;
    }



    protected void sendExtTimeEvent(long longPrimitive, String theString)
    {
        SupportBean theEvent = new SupportBean(theString, 0);
        theEvent.setLongPrimitive(longPrimitive);
        epService.getEPRuntime().sendEvent(theEvent);
    }
}
